package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ProjectListAdapter;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ProjectListBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.MyScrollView;
import com.wnhz.work.yz.view.NoScrollListView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ---项目详情List详情
 */
public class ProjectListActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private NoScrollListView my_listView1, my_listView2;
    private ProjectListAdapter mAdapter;
    private MyScrollView myScroll;
    private boolean isTrue1, isTrue2;

    private ImageView iv_right1, iv_right2;
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_list);
        initTitle();
        initData();
        initView();
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private String package_id, budget_id, goods_name;

    private void initData() {
        goods_name = getIntent().getStringExtra("goods_name");
        Log.e("----", goods_name + "");
        if (getIntent().getIntExtra("isYuShou", 0) == 0) {
            budget_id = getIntent().getStringExtra("budget_id");
            package_id = getIntent().getStringExtra("package_id");
            upLoad();
        } else {
            goods_id = getIntent().getStringExtra("goods_id");
            sku_id = getIntent().getStringExtra("sku_id");
            upLoad2();
        }

    }

    private String goods_id, sku_id;

    private void upLoad2() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("goods_id", goods_id);
        map.put("sku_id", sku_id);
        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }
        XUtil.Post(Url.BOOKING_BOOKINGPACKAGEPRICING, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        ProjectListBean bean = gson.fromJson(result, ProjectListBean.class);
                        ProjectListBean.InfoEntity infoBean = bean.getInfo();
                        cate_info = infoBean.getCate_info();
                        title.setText(goods_name);
                    } else {
                        title.setText("此套餐中无产品");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private List<ProjectListBean.InfoEntity.CateInfoEntity> cate_info = new ArrayList<>();

    private void upLoad() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("budget_id", budget_id);
        map.put("package_id", package_id);
        XUtil.Post(Url.BUDGET_GETBUDGETGOODSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String info = jsonObject.optString("info");
                    String resultNum = jsonObject.optString("result");
                    if (resultNum.equals("1")) {
                        ProjectListBean bean = gson.fromJson(result, ProjectListBean.class);
                        ProjectListBean.InfoEntity infoBean = bean.getInfo();
                        cate_info = infoBean.getCate_info();
                        title.setText(goods_name);
                    } else {
                        MyToast(info);
                        title.setText("此套餐中无产品");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void setData() {
        recycler.setAdapter(new BaseRVAdapter(this, cate_info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_cate_info;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.count, "共" + cate_info.get(position).getCount() + "套");
                holder.setTextView(R.id.cate_name, cate_info.get(position).getCate_name());
                RecyclerView recycler = holder.getView(R.id.item_recycler);
                recycler.setLayoutManager(new LinearLayoutManager(ProjectListActivity.this, LinearLayoutManager.VERTICAL, false));
                final List<ProjectListBean.InfoEntity.CateInfoEntity.DataEntity> data = cate_info.get(position).getData();
                recycler.setAdapter(new BaseRVAdapter(ProjectListActivity.this, data) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.project_list_item;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position) {
                        holder.setTextView(R.id.tv_title, data.get(position).getGoods_name());
                        x.image().bind(holder.getImageView(R.id.iv_img), data.get(position).getGoods_pic());

                        holder.setTextView(R.id.tv_numbers, "×" + data.get(position).getGoods_nums());
                        holder.setTextView(R.id.tv_type, data.get(position).getSku_info());
                        holder.setTextView(R.id.tv_price, "¥ " + data.get(position).getGoods_price());
                    }
                });

            }


        });


    }

    private void initView() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        findViewById(R.id.rl_open1).setOnClickListener(this);
//        findViewById(R.id.rl_open2).setOnClickListener(this);
//
//        iv_right1 = (ImageView) findViewById(R.id.iv_right1);
//        iv_right2 = (ImageView) findViewById(R.id.iv_right2);
//
//        my_listView1 = (NoScrollListView) findViewById(R.id.my_listView1);
//        my_listView2 = (NoScrollListView) findViewById(R.id.my_listView2);
        //   mAdapter = new ProjectListAdapter(ProjectListActivity.this, ProjectListActivity.this);
//        my_listView1.setAdapter(mAdapter);
//        my_listView2.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
//
//            //展开1
//            case R.id.rl_open1:
//                isTrue1 = !isTrue1;
//                if (isTrue1) {
//                    ObjectAnimator.ofFloat(iv_right1, "rotation", 0.0f, 180.0f).setDuration(300).start();
//                    my_listView1.setVisibility(View.VISIBLE);
//                    isTrue1 = true;
//                } else {
//                    ObjectAnimator.ofFloat(iv_right1, "rotation", 180.0f, 360.0f).setDuration(300).start();
//                    my_listView1.setVisibility(View.GONE);
//                    isTrue1 = false;
//                }
//                break;
//
//            //展开2
//            case R.id.rl_open2:
//                isTrue2 = !isTrue2;
//                if (isTrue2) {
//                    ObjectAnimator.ofFloat(iv_right2, "rotation", 0.0f, 180.0f).setDuration(300).start();
//                    my_listView2.setVisibility(View.VISIBLE);
//                    isTrue2 = true;
//                } else {
//                    ObjectAnimator.ofFloat(iv_right2, "rotation", 180.0f, 360.0f).setDuration(300).start();
//                    my_listView2.setVisibility(View.GONE);
//                    isTrue2 = false;
//                }
//                break;
//
//            //售后
//            case R.id.tv_sale:
//                intent = new Intent(this, CustomerServiceActivity.class);
//                startActivity(intent);
//                break;

            default:
                break;
        }
    }
}
